package com.example.security.jwt.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.security.jwt.service.UserService;

/**
 *
 */
@RequestMapping
@RestController
public class LoginController {

	private UserService userService;

	public LoginController(UserService userService) {
		this.userService = userService;
	}

	@PostMapping("/getUser")
	public String getUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		boolean authenticated = authentication.isAuthenticated();
		System.out.println("authenticated:" + authenticated);
		System.out.println("author:" + authentication.getAuthorities());
		System.out.println("getCredentials:" + authentication.getCredentials());
		System.out.println("getCredentials:" + authentication.getDetails());
		return "00";
	}

	@PreAuthorize("hasAuthority('user:resource1')")
	@GetMapping("/getResource")
	public String getResource() {
		return "资源";
	}

	@GetMapping("/getTest")
	public String getTest() {
		return userService.getTest();
	}

}
